package top.lyc829.iot_android.util;

import android.os.Looper;
import android.util.Log;
import android.widget.Toast;

import com.blankj.utilcode.util.SPUtils;

import top.lyc829.iot_android.MyApp;

/**
 * @Author lyc
 * @create 2021-05-30 17:32
 */
public class Utils {
    public static final String TAG="myTag";

    public static final String TAG_MQTT = "mqtt";

    /**
     * 打log工具
     * @param log 待打印的字符串
     */
    public static void Sout(String log) {
        Log.d(TAG, log);
    }

    public static void SoutMqtti(String log) {
        Log.i(TAG_MQTT, log);
    }

    public static void SoutMqttd(String log) {
        Log.d(TAG_MQTT, log);
    }

    public static void SoutMqtte(String log) {
        Log.e(TAG_MQTT, log);
    }

    public static void SoutMqtte(Exception exception) {
        if (exception != null) {
            SoutMqtte(exception.getMessage());
            exception.printStackTrace();
        }
    }

    /**
     * 打log工具
     * @param log 待打印的字符串
     */
    public static void SoutErr(String log) {
        Log.e(TAG, log);
    }


    public static void SoutInfo(String log) {
        Log.e(TAG, log);
    }


    public static boolean isLogin() {
        return !"-1".equals(SPUtils.getInstance().getString("token", "-1"));
    }

    /**
     * 显示toast
     * @param msg 待显示的字符串
     */
    public static void showToast(String msg) {
        Toast.makeText(MyApp.getContext(), msg, Toast.LENGTH_SHORT).show();
    }

    /**
     * 显示toast的同步版本，在子线程中使用
     * @param msg 待显示的字符串
     */
    public static void showToastSync(String msg) {
        Looper.prepare();
        Toast.makeText(MyApp.getContext(), msg, Toast.LENGTH_SHORT).show();
        Looper.loop();
    }


    /**
     * 判断字符串是否为空串
     * @param str 待判断的字符串
     * @return 如果为空串则返回true，否则返回false
     */
    public static boolean isStringEmpty(String str) {
        return str == null || str.length() <= 0;
    }

}
